/**
 * getRepairerTypeList   获取维修人员类型列表
 * queryRepairerTypeList 根据name模糊查询维修人员类型
 * addRepairerType       添加维修人员类型
 * getInfoRepairerType   获取维修人员类型信息
 * updateRepairerType    修改维修人员类型信息
 * deleteRepairerType    删除维修人员类型
 */
const repairTypeStatement = require("../../common/sql_sentences/repair_type")

const repairerStatement = require("../../common/sql_sentences/repairer")
const repairerTypeStatement = require("../../common/sql_sentences/repairer_type")

exports.getRepairerTypeList = async (req, res) => {
  try {
    let { page, pagesize } = req.query
    // 获取维修人员类型总数
    let total,
      sqlStr = repairerTypeStatement.selectCount()
    let results = await RunSQL(sqlStr)
    total = results[0].total

    if (!pagesize) {
      pagesize = total
    }

    // 获取维修人员类型列表
    sqlStr = repairerTypeStatement.selectLimit(page, pagesize)
    results = await RunSQL(sqlStr)
    return res.successSend("", 10000, { row: results, total })
  } catch (error) {
    res.errSend(error)
  }
}

exports.screenRepairerTypeList = async (req, res) => {
  try {
    // 获取维修人员列表
    sqlStr = `select hex(id) id, name from repairer_type`
    results = await RunSQL(sqlStr)
    return res.successSend("", 10000, results)
  } catch (error) {
    res.errSend(error)
  }
}

exports.queryRepairerTypeList = async (req, res) => {
  try {
    const data = req.query
    // 根据name模糊查询维修人员类型总数
    let total,
      sqlStr = repairerTypeStatement.selectCountLike(data.name)
    console.log(sqlStr)
    let results = await RunSQL(sqlStr)
    if (!results[0].total) return res.successSend("没有找到相关类型！", 10005)
    total = results[0].total

    // 根据name模糊查询获取维修人员类型列表
    sqlStr = repairerTypeStatement.selectLikeLimit(data.name, data.page, data.pagesize)
    console.log(sqlStr)
    results = await RunSQL(sqlStr)
    return res.successSend("", 10000, { row: results, total })
  } catch (error) {
    res.errSend(error)
  }
}

exports.addRepairerType = async (req, res) => {
  try {
    const data = req.body
    // 查询维修人员类型名称是否重复
    let sqlStr = repairerTypeStatement.select({ name: data.name })
    let results = await RunSQL(sqlStr)
    if (results.length) return res.successSend("名称已存在！", 10005)

    // 添加维修人员类型
    sqlStr = repairerTypeStatement.insert(req.body)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1)
      return res.successSend("添加维修人员类型失败！", 10005)
    return res.successSend("添加维修人员类型成功！")
  } catch (error) {
    res.errSend(error)
  }
}

exports.getInfoRepairerType = async (req, res) => {
  try {
    // 获取维修人员类型信息
    let sqlStr = repairerTypeStatement.idSelect(req.params.id)
    let results = await RunSQL(sqlStr)
    if (results.length !== 1)
      return res.successSend("获取维修人员类型信息失败！", 10005)
    return res.successSend("", 10000, results[0].name)
  } catch (error) {
    res.errSend(error)
  }
}

exports.updateRepairerType = async (req, res) => {
  try {
    const data = req.body
    // 查询维修人员类型名称是否重复
    let sqlStr = repairerTypeStatement.select({ name: data.name })
    let results = await RunSQL
    if (results[0]?.id && results[0]?.id !== data.id)
      return res.errSend("名称已存在！", 10005)

    // 修改维修人员类型信息
    sqlStr = repairerTypeStatement.idUpdate(data, data.id)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1)
      return res.successSend("修改维修人员类型信息失败！", 10005)
    return res.successSend("修改维修人员类型信息成功！")
  } catch (error) {
    res.errSend(error)
  }
}

exports.deleteRepairerType = async (req, res) => {
  try {
    const id = req.params.id
    // 获取报修类型
    let sqlStr = repairTypeStatement.select({ r_type_id: id })
    let results = await RunSQL(sqlStr)
    if (results.length) return res.successSend("删除维修人员类型失败!有报修类型属于该维修人员类型！", 10005)

    // 获取维修人员信息
    sqlStr = repairerStatement.select({ type_id: id })
    results = await RunSQL(sqlStr)
    if (results.length) return res.successSend("删除维修人员类型失败!有维修人员属于该维修人员类型！", 10005)

    // 删除维修人员类型
    sqlStr = repairerTypeStatement.delete(id)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1)
      return res.successSend("删除维修人员类型失败！", 10005)
    return res.successSend("删除维修人员类型信息成功！")
  } catch (error) {
    res.errSend(error)
  }
}
